﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AudioCore.Entity;
using AudioCore.DAO;
using System.Data;

namespace AudioCore.Biz
{
    public class ArticleBiz : BaseBiz
    {
        public int AddArticle(ArticleEntity entity)
        {
            try
            {
                using (ArticleDao dao = new ArticleDao())
                {
                    return dao.InsertEntity(entity, typeof(ArticleEntity));
                }
            }
            catch (Exception e)
            {
                //do nothing
            }
            return -1;
        }

        public int UpdateArticle(ArticleEntity entity)
        {
            try
            {
                using(ArticleDao dao = new ArticleDao())
                {
                    return dao.UpdateEntity(entity, typeof(ArticleEntity));
                }
            }
            catch (Exception e)
            {
                //do nothing
            }
            return -1;
        }

        public int DeleteArticle(int articleID)
        {
            try
            {
                using (ArticleDao dao = new ArticleDao())
                {
                    return dao.DeleteEntity(articleID);
                }
            }
            catch (Exception e)
            {
                //do nothing
            }
            return -1;
        }

        public int UpdateDeletedStatus(int articleID, bool deletedStatus)
        {
            try
            {
                using (ArticleDao dao = new ArticleDao())
                {
                    return dao.UpdateDeleteStatus(articleID, deletedStatus);
                }
            }
            catch (Exception e)
            {
                //do nothing
            }
            return -1;
        }

        public int ChangeActived(int articleID, bool actived)
        {
            try
            {
                using (ArticleDao dao = new ArticleDao())
                {
                    string[] columns = new string[] {"Actived"};
                    object[] values = new object[] {actived};
                    return dao.UpdateColumnsByID(articleID, columns, values);
                }
            }
            catch (Exception e)
            {
                //do nothing
            }
            return -1;
        }

        #region Method for service
        public ArticleEntity GetArticleByID(int articleID)
        {
            try
            {
                using (ArticleDao dao = new ArticleDao())
                {
                    DataTable table = dao.GetArticleByID(articleID);
                    if (table.Rows.Count > 0)
                    {
                        return (ArticleEntity)BaseBiz.MapDataRowToObject(table.Rows[0], typeof(ArticleEntity));
                    }
                }
            }
            catch (Exception e)
            {
                //do nothing
            }
            return null;
        }

        public List<ArticleEntity> GetArticleByCategoryID(int categoryID)
        {
            try
            {
                using (ArticleDao dao = new ArticleDao())
                {
                    DataTable table = dao.GetArticleByCategoryID(categoryID);
                    if (table.Rows.Count > 0)
                    {
                        return BaseBiz.MapDataTableToList(table, typeof(ArticleEntity)).ConvertAll(items => (ArticleEntity)items);
                    }
                }
            }
            catch (Exception e)
            {
                //do nothing
            }
            return new List<ArticleEntity>();
        }

        public List<ArticleEntity> GetArticleByStatus(bool deletedStatus)
        {
            try
            {
                using (ArticleDao dao = new ArticleDao())
                {
                    DataTable table = dao.GetArticle(deletedStatus);
                    if (table.Rows.Count > 0)
                    {
                        return BaseBiz.MapDataTableToList(table, typeof(ArticleEntity)).ConvertAll(items => (ArticleEntity)items);
                    }
                }
            }
            catch (Exception e)
            {
                //do nothing
            }
            return new List<ArticleEntity>();
        }

        public List<ArticleEntity> GetListArticleByPage(int accountID, int categoryID, int pageIndex, int pageSize, ref int counter)
        {
            try
            {
                using (ArticleDao dao = new ArticleDao())
                {
                    DataTable table = dao.GetArticleByPage(accountID, categoryID, pageIndex, pageSize, ref counter);
                    if (table.Rows.Count > 0)
                    {
                        return BaseBiz.MapDataTableToList(table, typeof(ArticleEntity)).ConvertAll(item => (ArticleEntity)item);
                    }
                }
            }
            catch (Exception e)
            {
                //do nothing
            }
            return new List<ArticleEntity>();
        }

        public List<ArticleEntity> GetArticleByPaging(string keyWord, int categoryID, bool? active, int pageIndex, int pageSize, out int recordCount)
        {
            try
            {
                using (ArticleDao dao = new ArticleDao("VIEW_AUDIO_Article"))
                {
                    string[] fieldsName = new string[] { "*" };
                    string[] equalColumn = new string[] { "CategoryID", "Actived" };
                    object[] equalValue = new object[] { categoryID, active };
                    string[] searchColumn = new string[] { "ArticleID", "Artist", "Author" };

                    DataTable table = dao.SearchEntityByPaging(fieldsName, equalColumn, equalValue, searchColumn, keyWord, pageIndex, pageSize, "SortOrder", out recordCount);
                    if (table.Rows.Count > 0)
                    {
                        return BaseBiz.MapDataTableToList(table, typeof(ArticleEntity)).ConvertAll(item => (ArticleEntity)item);
                    }
                }
            }
            catch (Exception ex)
            {
            }
            recordCount = 0;
            return new List<ArticleEntity>();
        }

        public List<ArticleEntity> GetArticleByPaging(string keyword, int pageIndex, int pageSize, out int recordCount)
        {
            try
            {
                using (ArticleDao dao = new ArticleDao())
                {
                    string[] fieldsName = new string[] { "*" };
                    string[] equalColumn = null;
                    object[] equalValue = null;
                    string[] searchColumn = new string[] { "ArticleID", "ArticleName" };

                    DataTable table = dao.SearchEntityByPaging(fieldsName, equalColumn, equalValue, searchColumn, keyword, pageIndex, pageSize, "ArticleID", out recordCount);
                    if (table.Rows.Count > 0)
                    {
                        return BaseBiz.MapDataTableToList(table, typeof(ArticleEntity)).ConvertAll(item => (ArticleEntity)item);
                    }
                }
            }
            catch (Exception ex)
            {
            }
            recordCount = 0;
            return new List<ArticleEntity>();
        }

        public List<ArticleEntity> GetDeletedArticle(int pageIndex, int pageSize, out int recordCount)
        {
            try
            {
                using (ArticleDao dao = new ArticleDao())
                {

                    DataTable table = dao.GetDeletedArticle(pageIndex, pageSize, out recordCount);
                    if (table.Rows.Count > 0)
                    {
                        return BaseBiz.MapDataTableToList(table, typeof(ArticleEntity)).ConvertAll(item => (ArticleEntity)item);
                    }
                }
            }
            catch (Exception ex)
            {
            }
            recordCount = 0;
            return new List<ArticleEntity>();
        }

        public List<ArticleEntity> GetTopArticle(int accountID, int numberArticle)
        {
            try
            {
                using (ArticleDao dao = new ArticleDao())
                {
                    DataTable table = dao.GetTopArticle(accountID, numberArticle);
                    if (table.Rows.Count > 0)
                    {
                        return BaseBiz.MapDataTableToList(table, typeof(ArticleEntity)).ConvertAll(item => (ArticleEntity)item);
                    }
                }
            }
            catch (Exception e)
            {
                //do nothing
            }
            return new List<ArticleEntity>();
        }
        public List<ArticleEntity> SearchListArticle(int accountID, string keyWord, int pageIndex, int pageSize, ref int counter)
        {
            try
            {
                using (ArticleDao dao = new ArticleDao())
                {
                    DataTable table = dao.SearchArticle(accountID, keyWord, pageIndex, pageSize, ref counter);
                    if (table.Rows.Count > 0)
                    {
                        return BaseBiz.MapDataTableToList(table, typeof(ArticleEntity)).ConvertAll(item => (ArticleEntity)item);
                    }
                }
            }
            catch (Exception e)
            {
                //do nothing
            }
            return new List<ArticleEntity>();
        }

        /// <summary>
        /// Get list favorite article of an user
        /// </summary>
        /// <param name="accountID"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="counter"></param>
        /// <returns></returns>
        public List<ArticleEntity> GetListFavoriteArticleModel(int accountID, int pageIndex, int pageSize, ref int counter)
        {
            try
            {
                using (ArticleDao daoArticle = new ArticleDao())
                {
                    DataTable table = daoArticle.GetListFavoriteArticle(accountID, pageIndex, pageSize, ref counter);
                    if (table.Rows.Count > 0)
                    {
                        return BaseBiz.MapDataTableToList(table, typeof(ArticleEntity)).ConvertAll(item => (ArticleEntity)item);
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return new List<ArticleEntity>();
        }

        public int RateArticle(int articleID, int ratePoint)
        {
            try
            {
                using (ArticleDao dao = new ArticleDao())
                {
                    string[] columns = new string[] { "RatePoint" };
                    object[] values = new object[] { ratePoint };
                    return dao.UpdateColumnsByID(articleID, columns, values);
                }
            }
            catch (Exception e)
            {
                //do nothing
            }
            return -1;
        }


        public int IncreasePlayCounter(int articleID)
        {
            try
            {
                using (ArticleDao dao = new ArticleDao())
                {
                    return dao.IncreasePlayCounter(articleID);
                }
            }
            catch (Exception e)
            {
                //do nothing
            }
            return -1;
        }

        #endregion

    }
}
